import React from "react";
import * as UI from "antd-mobile"
import * as ICON from "antd-mobile-icons"
import request from '@/services/request'
import cookies from 'react-cookies'

// React命名空间下创建自定义属性
React.UI = UI
React.ICON = ICON
React.request = request
React.cookies = cookies
React.business = {}


// 成功和失败提醒方法
React.toast = (msg: string, callback?: any, type?: string) => 
{
    React.UI.Toast.show({
        icon: type ? type : 'success',
        content: msg,
        duration: 1500,
        afterClose: () => 
        {
            if(!callback) return;

            if(typeof callback === "string") callback === "back" ? React.navigate(-1) : React.navigate(callback)
            else callback()
        }
    })
}

//登录状态
React.check = async()=>{
    if(React.business.id)return true
    const token = cookies.load('token')
    if(!token){
        return false
    }
    let result = await request.get(`/business/check?token=${token}`)
    if(result.code==1){
        return true
    }else{
        cookies.remove('token');
        return false
    }
}

React.RouterRules = [
    '/',
    '/coupon/info',
    '/business/login',
    '/room/info'
]
export default {}